package com.spring.sys.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.spring.sys.dao.SysRoleDao;
import com.spring.sys.dao.SysRoleMenuDao;
import com.spring.sys.po.SysRole;
import com.spring.sys.service.ISysRoleService;

@Service
public class SysRoleServiceImpl implements ISysRoleService {

	@Autowired
	private SysRoleDao sysRoleDao;

	@Autowired
	private SysRoleMenuDao sysRoleMenuDao;

	@Override
	public List<SysRole> findRoleList() {
		// TODO Auto-generated method stub
		return sysRoleDao.find("findRoleList", null);
	}

	@Override
	public SysRole selectByPrimaryKey(Integer id) {
		// TODO Auto-generated method stub
		return sysRoleDao.get("selectByPrimaryKey", id);
	}

	@Override
	public void saveOrUpdate(SysRole sysRole,String menuIds) {
		// TODO Auto-generated method stub
		if (sysRole.getId() == null) {
			sysRole.setStatus(1);
			sysRoleDao.insert(sysRole);
			
		} else {
			sysRoleDao.update("updateByPrimaryKeySelective", sysRole);
			sysRoleMenuDao.delete("deleteByRoleId",sysRole.getId());
		}
		
		List<String> list = Arrays.asList(menuIds.split(","));
		Map<String,Object> map = new HashMap<>();
		map.put("roleId", sysRole.getId());
		map.put("menuIds", list);
		sysRoleMenuDao.insert("batchInsert",map);
	}

	@Override
	public void delSysRole(Integer id) {
		// 删除该角色对应的菜单关系
		sysRoleMenuDao.delete("deleteByRoleId", id);
		// 删除该角色
		sysRoleDao.delete("deleteByPrimaryKey", id);
	}

}
